package com.zzyl.nursing.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.util.StringUtil;
import com.zzyl.common.core.domain.entity.SysUser;
import com.zzyl.common.core.domain.model.LoginUser;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.dto.AlertDataDto;
import com.zzyl.system.mapper.SysUserMapper;
import com.zzyl.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;
/**
 * 报警数据Service业务层处理
 */
@Service
@Slf4j
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper,AlertData> implements IAlertDataService
{
    @Autowired
    private AlertDataMapper alertDataMapper;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询报警数据
     *
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id)
    {
        AlertData alertData = getById(id);
        return alertData;
    }

    /**
     * 查询报警数据列表
     *
     * @param alertDataDto 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertDataDto alertDataDto)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
         alertDataDto.setUserId(loginUser.getUserId());
        return  alertDataMapper.selectAlertDataList(alertDataDto);
    }

    /**
     * 新增报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData)
    {
        return save(alertData)?1:0;
    }

    /**
     * 修改报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData)
    {
        return updateById(alertData)?1:0;
    }

    /**
     * 批量删除报警数据
     *
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids))?1:0;
    }

    /**
     * 删除报警数据信息
     */
    @Override
    public int deleteAlertDataById(Long id)
    {
        return removeById(id)?1:0;
    }

    /**
     * 处理报警设备数据
     *
     * @param alertData
     */
    @Override
    public int handleAlertData(AlertData alertData) {
        // 1获取id
        AlertData oldAlertData = getById(alertData.getId());
        // 1.1判断数据是否存在
        if (ObjUtil.isEmpty(oldAlertData)) {
            log.warn("报警数据不存在id={}", alertData.getId());
            return 0;
        }
        String accessLocation = oldAlertData.getAccessLocation();
        log.info("获取接入位置", accessLocation);
        // 2.更新时间
        alertData.setUpdateTime(new Date());
        //3.修改状态为1
        alertData.setStatus(1);
        // 4.获取处理人id
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserId();
        alertData.setProcessorId(userId);
        // 5.获取处理人姓名
        SysUser sysUser = sysUserMapper.selectUserById(userId);
        //6.判断是否存在
        if (sysUser == null) {
            log.warn("处理人不存在");
        } else {
            alertData.setProcessorName(sysUser.getNickName());
        }
        // 7. 数据插入数据库
        alertDataMapper.updateAlertData(alertData);
        log.info("处理报警数据成功{}", alertData.getId());
        return 1;
    }
}
